// component/tree-slider/index.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        treeList:Array
    },

    /**
     * 组件的初始数据
     */
    data: {
        lock:false,
        firstList:[],
        secondList:[],
        actives: [],
        firstActives: [],
        secondActives: [],
        isShow: false,
        isScroll: false
    },

    /**
     * 组件的方法列表
     */
    methods: {
        myCatchTouch() {
            console.log("-------")
            return;
        },
        clickNav:function(event){
            if (this.data.lock) {
                return;
            }
            this.lockScreen();
            var index = event.currentTarget.dataset.index; 
            this.updateList(index)
            this.triggerEvent('click-nav', {index:index})
            this.unLockScreen();
        },
        firstClickItem:function(event){
            var index = event.currentTarget.dataset.index;
            let actives = this.data.firstActives;
            actives.forEach(function (item, i) {
                actives[i] = false;
            });
            actives[index] = true;
            this.setData({
                firstActives: actives
            });
            let secondList = this.data.firstList[index].SubLevelList;
            this.setData({
                secondList: secondList
            });
            this.triggerEvent('click-first', { index: index })
            this.setSecondLevelActive()
        },
        secondClickItem:function(event){
            var index = event.currentTarget.dataset.index;
            let actives = this.data.secondActives;
            actives.forEach(function (item, i) {
                actives[i] = false;
            });
            actives[index] = true;
            this.setData({
                secondActives: actives
            });
            this.triggerEvent('click-second', { index: index })
        },
        //禁用屏幕点击，防止在做动画时误触
        lockScreen() {
            this.setData({
                lock: true
            });
        },
        unLockScreen() {
            let that = this;
            setTimeout(function () {
                that.setData({
                    lock: false
                })
            }, 800);
        },
        //隐藏侧边栏
        hideClick() {
            if (this.data.lock) {
                return;
            }
            this.lockScreen();
            let that = this;
            this.setData({
                isAnima: false
            });
            setTimeout(function () {
                that.setData({
                    isShow: false
                });
            }, 600);
            this.unLockScreen();
        },
        //更新一级列表和二级的数据
        updateList:function(index){
            let that = this
            this.data.treeList.forEach(function (item, i) {
                that.data.treeList[i].active = false
            });
            this.data.treeList[index].active = true;
            let firstLevelList = this.data.treeList[index].SubLevelList ? this.data.treeList[index].SubLevelList : null;
            let secondList = firstLevelList[0] ? firstLevelList[1].SubLevelList : null;
            let isShow = firstLevelList.length > 0 ? true : false;
            let isAnima = firstLevelList.length > 0 ? true : false;
            this.setData({
                treeList: this.data.treeList,
                isShow: isShow,
                isAnima: isAnima,
                firstList: firstLevelList,
                secondList: secondList,
                positionName: this.data.treeList[index].PositionName
            });
            this.setFirstLeveActive(firstLevelList);
            this.setSecondLevelActive(secondList);
        },
        //清除一级列表所有的选中效果
        setFirstLeveActive(list) {
            let actives = [];
            if (list) {
                for (let i = 0; i < list.length; i++) {
                    actives.push(false);
                }
                actives[1] = true;
                this.setData({
                    firstActives: actives
                })
            }
        },
        //清除二级列表的选中效果
        setSecondLevelActive(list) {
            let actives = [];
            if (list) {
                for (let i = 0; i < list.length; i++) {
                    actives.push(false);
                }
                this.setData({
                    secondActives: actives
                })
            }
        },
    }
})
